<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use PHPUnit\Util\RegularExpressionTest;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class DormType.
 *
 * @package namespace App\Models;
 */
class DormType extends BaseModel implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'company_id',
        'name',
        'staff_position_id',
        'electric_fee_type_id',
        'water_fee_type_id',
        'manage_fee_type_id',
        'hot_water_fee_type_id',
        'for_gender'
    ];

    public function staffPosition()
    {
        return $this->belongsTo(StaffPosition::class);
    }

    public function electricFeeType()
    {
        return $this->belongsTo(ElectricFeeType::class);
    }


    public function hotWaterFeeType()
    {
        return $this->belongsTo(HotWaterFeeType::class);
    }

    public function waterFeeType()
    {
        return $this->belongsTo(WaterFeeType::class);
    }

    public function manageFeeType()
    {
        return $this->belongsTo(ManageFeeType::class);
    }

    public function depositTypes()
    {
        return $this->belongsToMany(DepositType::class);
    }


    public function scopeElectricFeeCalcByUnit(Builder $builder)
    {
        return $builder->whereHas('electricFeeType', function ($q) {
            $q->where('compute_unit', '=', '元/计量单位');
        });
    }

    public function scopeWaterFeeCalcByUnit(Builder $builder)
    {
        return $builder->whereHas('waterFeeType', function ($q) {
            $q->where('compute_unit', '=', '元/计量单位');
        });
    }

    public function buildings()
    {
        return $this->hasMany(Building::class);
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (auth()->check()) {
                $model->company_id = auth()->user()->company_id ?? null;
            }
        });
    }
}
